package com.dunqi.gpm.shunqing.home.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.base.BaseAct;
import com.dunqi.gpm.shunqing.common.util.JsonUtils;
import com.dunqi.gpm.shunqing.common.util.TimeUtil;
import com.dunqi.gpm.shunqing.common.view.NoScrollGridView;
import com.dunqi.gpm.shunqing.develop.widget.DrawLeftRadioButton;
import com.dunqi.gpm.shunqing.home.adapter.PhotoAdapter;
import com.dunqi.gpm.shunqing.home.bean.EarlyInfo;
import com.dunqi.gpm.shunqing.home.bean.RemarkPic;
import com.dunqi.gpm.shunqing.home.widget.NormalView;
import com.dunqi.gpm.shunqing.my.util.UserUtil;
import com.foamtrace.photopicker.intent.PhotoPreviewIntent;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

import static com.dunqi.gpm.shunqing.home.constance.Constance.REQUEST_PREVIEW_CODE;

public class AuditDeatilActivity extends BaseAct implements AdapterView.OnItemClickListener {

    private static final String DATA = "data";
    @BindView(R.id.state_name_view)
    NormalView stateNameView;
    @BindView(R.id.plan_start_time_view)
    NormalView planStartTimeView;
    @BindView(R.id.plan_complete_time_view)
    NormalView planCompleteTimeView;
    @BindView(R.id.remark_tv)
    TextView remarkTv;
    @BindView(R.id.no_grid_view)
    NoScrollGridView noGridView;
    @BindView(R.id.state_view)
    NormalView stateView;
    @BindView(R.id.config_btn)
    DrawLeftRadioButton configBtn;

    ArrayList<String> data = new ArrayList<>();

    private EarlyInfo item;
    private PhotoAdapter adapter;

    String[] stageStatuss = new String[]{"待申请", "待完成", "已完成"};

    String url = "http://pic4.nipic.com/20091217/3885730_124701000519_2.jpg";

    @Override
    protected void refreshData(Bundle savedInstanceState) {

    }

    @Override
    protected void initUiAndListener() {
        item = (EarlyInfo) getIntent().getSerializableExtra(DATA);


        refreshUI();

        noGridView.setOnItemClickListener(this);

    }

    private void refreshUI() {
        stateNameView.setText(item.getStageName());
        int status = item.getStageStatus();

        if (status == 1) {
            stateView.setTextColor(R.color.material_deep_orange_400);
        } else {
            configBtn.setVisibility(View.GONE);
        }
        stateView.setText(stageStatuss[item.getStageStatus()]);

        planStartTimeView.setText(TimeUtil.getTen(item.getPlanStartTime()));
        planCompleteTimeView.setText(TimeUtil.getTen(item.getPlanCompleteTime()));

        remarkTv.setText(item.getRemark());

        String auditSubmitAttachs = item.getAuditSubmitAttachs();
        if (!TextUtils.isEmpty(auditSubmitAttachs)) {
            ArrayList<RemarkPic> pics = JsonUtils.fromJson(auditSubmitAttachs, new TypeToken<ArrayList<RemarkPic>>() {
            }.getType());
            data.clear();
            for (RemarkPic pic : pics) {
                data.add(UserUtil.getBaseUrl() + pic.getUrl());
            }

            adapter = new PhotoAdapter(getActivity(), data);
            noGridView.setAdapter(adapter);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode ==365) {
            setResult(365);
            finish();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_audit_deatil;
    }


    public static void start(Activity context, EarlyInfo earlyInfo) {
        Intent intent = new Intent(context, AuditDeatilActivity.class);
        intent.putExtra(DATA, earlyInfo);
        context.startActivityForResult(intent,611);
    }


    @OnClick(R.id.config_btn)
    public void onViewClicked() {
        String stageName = item.getStageName();
        long proAnnualPlanPreStageId = item.getProAnnualPlanPreStageId();
        CompleteEarlyInfoActivity.start(getActivity(), proAnnualPlanPreStageId, stageName);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        PhotoPreviewIntent intent = new PhotoPreviewIntent(this);
        intent.setCurrentItem(position); // 当前选中照片的下标
        intent.setPhotoPaths(data); // 已选中的照片地址
        startActivityForResult(intent, REQUEST_PREVIEW_CODE);
    }
}
